<?php

class Zend_Controller_Action_Helper_User extends Zend_Controller_Action_Helper_Abstract {
	
	public function direct() {
		
	}
	
	/**
	 * Check if user is already loggin.
	 */
	public function isLoggedIn() {
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		return isset($data);
	}
	
	/**
	 * Check the given user info is same as the login session data.
	 * Then we can say this user is logged in
	 * 
	 * @param $user_id
	 * @return return true if given user has logged in
	 */
	public function userHasLoggedIn($user_id) {
		$hasLoggedIn = false;
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		if ($data->id == $user_id) {
			$hasLoggedIn = true;
		}
		return $hasLoggedIn;
	}
	
	/**
	 * Return user data from session.
	 */
	public function getUserData() {
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		return $data;
	}
	
	/**
	 * @param $user user
	 * @return return username if any, otherwise, return email
	 */
	public function getUserPresentName($user) {
		if (isset($user->name) && $user->name != '') {
			return $user->name;
		} else {
			return $user->email;
		}
	}
	
	/**
	 * Get user activate date.
	 * 
	 * @param $user
	 */
	public function getActivateDate($user) {
		if (isset($user->activated) && $user->activated != '') {
			return $user->activated;
		} else {
			return "Not specified";
		}
	}
	
	/**
	 * Check if user has logged in with facebook.
	 * 
	 * @return return array loggedIn=>true|false and facebook object, facebook user if any
	 */
	public function isLoggedInFacebookUser() {
		$config = Zend_Registry::get("config");
        $facebook = new Facebook(array(
        'appId'  => $config->facebook->app->appId,
        'secret' => $config->facebook->app->secretKey,
        'cookie' => true,
        'domain' => $config->facebook->app->domain
        ));
        
        $ret = array(
            "loggedIn" => false,
            "facebook" => $facebook
        );
        
        // We may or may not have this data based on a $_GET or $_COOKIE based session.
        //
        // If we get a session here, it means we found a correctly signed session using
        // the Application Secret only Facebook and the Application know. We dont know
        // if it is still valid until we make an API call using the session. A session
        // can become invalid if it has already expired (should not be getting the
        // session back in this case) or if the user logged out of Facebook.
        $session = $facebook->getSession();
        
        $logger = Zend_Registry::get("logger");
        $me = null;
        // Session based API call.
        if ($session) {
            try {
                $me = $facebook->api('/me');
                $ret["facebookUser"] = $me;
            } catch (FacebookApiException $e) {
            	$logger->log($e->__toString(), Zend_Log::WARN);
                //throw new Zend_Exception("Error to request user info. ".$e->__toString());
            }
        }
        $ret["loggedIn"] = !empty($me);
        return $ret;
	}
}
?>